﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Logging
{
    public class LoggerBase : ILogger
    {
        private StringBuilder builer = new StringBuilder();
        private LogLevel LogLevel = LogLevel.None;

        public LoggerBase()
        {
            InitLevel();
        }

        private void InitLevel()
        {
            var level = ConfigurationManager.AppSettings["LogLevel"] ?? string.Empty;
            level = level.ToLower();
            switch (level)
            {
                case "all":
                    LogLevel = LogLevel.All;
                    break;
                case "debug":
                    LogLevel = LogLevel.Debug;
                    break;
                case "info":
                    LogLevel = LogLevel.Info;
                    break;
                case "error":
                    LogLevel = LogLevel.Error;
                    break;
                case "global":
                    LogLevel = LogLevel.Global;
                    break;
                default:
                    LogLevel = LogLevel.None;
                    break;
            }
        }

        #region ILogger Members

        public string Name
        {
            get;
            set;
        }

        public virtual void Log(string logInfo)
        {
            Log(logInfo, LogLevel.Global);
        }

        public virtual void Log(string logInfo, LogLevel level)
        {
            if (level >= LogLevel)
            {
                builer.AppendLine(LogBuilder.BuildLog(logInfo, Name, Enum.GetName(level.GetType(), level)));
            }
        }

        public virtual void Clear()
        {
            builer = new StringBuilder();
        }

        public virtual void Debug(string logInfo)
        {
            Log(logInfo, LogLevel.Debug);
        }

        public virtual void Info(string logInfo)
        {
            Log(logInfo, LogLevel.Info);
        }

        public virtual void Error(string logInfo)
        {
            Log(logInfo, LogLevel.Error);
        }

        public virtual string GetLog()
        {
            return builer.ToString();
        }
        #endregion

    }
}
